package com.qianfeng.missfood.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.qianfeng.missfood.R;
import com.qianfeng.missfood.bean.BootScreenInfoEntity;
import com.qianfeng.missfood.bean.SplashContent;
import com.qianfeng.missfood.constants.UrlConstants;

public class SplashActivity extends AppCompatActivity {

    private RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        init();

    }

    private void init() {

        relativeLayout = (RelativeLayout) findViewById(R.id.splash_bg);
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.GET, UrlConstants.startUrl, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                String result = responseInfo.result;
                SplashContent splashContent = JSON.parseObject(result, SplashContent.class);
                BootScreenInfoEntity bootScreenInfoEntity = splashContent.getBootScreenInfo();
                bootScreenInfoEntity.setUrl("http://image.mishi.cn/sysstartpic/2015/0821/0_613719a5-3874-428e-bdf0-c4773391a1cb.jpg");
                String url = bootScreenInfoEntity.getUrl();

                BitmapUtils bitmapUtils = new BitmapUtils(SplashActivity.this);
                bitmapUtils.display(relativeLayout, url);
                startAni();
            }

            @Override
            public void onFailure(HttpException e, String s) {

                Toast.makeText(SplashActivity.this, "网络连接异常", Toast.LENGTH_LONG).show();
            }
        });


    }

    private void startAni() {


        AnimationSet animationSet = new AnimationSet(true);
        ScaleAnimation scaleAnimation = new ScaleAnimation(1.2f, 1.3f, 0.9f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animationSet.addAnimation(scaleAnimation);
        animationSet.setDuration(1000);
        animationSet.setFillAfter(true);
        animationSet.setStartOffset(1000);
        relativeLayout.startAnimation(animationSet);
        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                startAction();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {


            }
        });

    }

    private void startAction() {

        Intent intent = new Intent(this, GuideActivity.class);
        startActivity(intent);
        this.finish();
    }

    @Override
    public void onBackPressed() {

        startAction();

    }
}
